package com.trkj.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.ArchiveDAO;
import com.trkj.dao.ContractDAO;
import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.StandardDAO;
import com.trkj.enmu.EmpType;
import com.trkj.pojo.Archive;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Standard;
import com.trkj.vo.StandardVo;

@Service
@Transactional
public class StandardService {
	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	StandardDAO standardDAO;
	@Autowired
	ContractDAO contractDAO;
	@Autowired
	ArchiveDAO archiveDAO;
	@Autowired
	WorkFlowService workFlowService;

	// emp.emp_id,emp.emp_name,emp.emp_entertime,dept.dept_id,dept.dept_name,employs.employ_probationary
	public StandardVo standardVo(Integer empId) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Emp emp = empDAO.findOne(empId);
			StandardVo standardVo = new StandardVo();
			standardVo.setEmpId(emp.getEmpId());
			standardVo.setEmpName(emp.getEmpName());
			standardVo.setStandardEntertime(simpleDateFormat.format(emp.getEmpEntertime()));
			if (emp.getDept() != null) {
				standardVo.setDeptId(emp.getDept().getDeptId());
				standardVo.setDeptName(emp.getDept().getDeptName());
			}
			String time = contractDAO.findTime(empId);
			standardVo.setStandardTrialtime(time);
			Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
			int a = date.getYear() + 1900;
			int b = date.getMonth() + 1;
			int c = date.getDate();
			standardVo.setStandardUptime(a + "-" + b + "-" + c);
			standardVo.setLunchPeople(emp.getEmpId());
			return standardVo;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 保存转正申请
	 * 
	 * @param standard
	 * @param empId
	 * @param deptId
	 * @return
	 */
	public Integer save_standard(Standard standard, Integer empId, Integer deptId) {
		try {
			if (standard != null) {
				if (empId != null) {
					Emp emp = empDAO.findOne(empId);
					standard.setEmp(emp);
				}
				if (deptId != null) {
					Dept dept = deptDAO.findOne(deptId);
					standard.setDept(dept);
				}
				return standardDAO.save(standard).getStandardId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 通过流程id完成业务（流程端）
	 * 
	 * @param standrdId
	 * @return
	 */
	public boolean updatestandardtime(Integer standrdId) {
		try {
			if (standrdId != null) {
				Standard standard = standardDAO.findOne(standrdId);
				Archive archive = standard.getEmp().getArchive();
				if (archive != null) {
					archive.setArchivesEmptype(EmpType.regularemployee);
					archive.setArchivesPositivetime(standard.getStandardTime());
					return true;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 判断该员工是否可以转正
	 * @param empId
	 * @return
	 */
	public boolean isStandard(Integer empId) {
		try {
			Emp emp = empDAO.findOne(empId);
			if (emp != null) {
				Archive archive = emp.getArchive();
				if (archive != null) {
					if (archive.getArchivesEmptype() == EmpType.trainee) {
						List<String> unFinishedProcessIds = workFlowService.getUnFinishedProcessIds("standard_act", empId);
						if (unFinishedProcessIds==null || unFinishedProcessIds.size()==0) {
							return true;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
